//
//  BombIndicator.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 8/22/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "BombIndicator.h"

@implementation BombIndicator

-(id) initWithBomb:(Bomb *)bomb
{
    if (self = [super initWithSpriteFrameName:[self getSpriteFrameName]])
    {
        mBomb = bomb;
        mIsActiveBomb = NO;
        [self setColor:CL_BOMB_INDICATOR];
        [self setIgnoreBodyRotation:NO];
        [self createBody];
        [self scheduleUpdate];
    }
    
    return self;
}

-(void) activeBomb
{
    if (mIsActiveBomb)
        return;
    
    mIsActiveBomb = YES;
    CCTintTo *tintTo = [CCTintTo actionWithDuration:1.0 red:255 green:255 blue:255];
    tintTo = [CCEaseExponentialOut actionWithAction:tintTo];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        if (mBomb._state == eBS_IDLE)
        {
            [mBomb explode];
            [self deactiveBombIndicator];
        }
    }];
    CCSequence *sequence = [CCSequence actions:tintTo, callBlock, nil];
    [self runAction:sequence];
    
    CCTintTo *tintTo2 = [CCTintTo actionWithDuration:1.0 red:255 green:255 blue:255];
    tintTo2 = [CCEaseExponentialOut actionWithAction:tintTo2];
    CCSequence *sequence2 = [CCSequence actions:tintTo2, nil];
    [mBomb runAction:sequence2];
}

-(void) update:(ccTime)delta
{
    if (mBomb != nil)
    {
        [self setPosition:[mBomb getRealPosition]];
        if (mBomb._state != eBS_IDLE)
        {
            [self deactiveBombIndicator];
        }
    }
}

-(void) deactiveBombIndicator
{
    [self setVisible:NO];
    [self unscheduleUpdate];
    _b2Body->SetActive(false);
}

-(void) createBody
{
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(0, 0);
    
    _b2Body = PhysicSystem::getInstance()->mWorld->CreateBody(&bodyDef);
    
    b2CircleShape shape;
    shape.m_radius = (mBomb.contentSize.width * 1) / PTM_RATIO;
    
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &shape;
    fixtureDef.filter.categoryBits = [self getObjectType];
    fixtureDef.filter.maskBits = MASK_BIT_BOMB_RANGE;
    fixtureDef.isSensor = true;
    _b2Body->CreateFixture(&fixtureDef);
    
    [self setPTMRatio:PTM_RATIO];
    _b2Body->SetUserData(self);
}

-(OBJECT_TYPE) getObjectType
{
    
    return eO_BOMB_RANGE;
}

-(void) addActions:(NSArray *)actions
{
    
}

-(void) startAction
{
    
}

-(void) stopAction
{
    [self stopAllActions];
}

-(NSString *) getSpriteFrameName
{
    return SFN_BOMB_INDICATOR;
}

-(void) dealloc
{
    
    [super dealloc];
    CCLOG(@"Bomb indicator dealloc!");
}


@end
